/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.notaria.guerrasalas.bc;

import gob.satt.comun.jpa.Command;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import oracle.toplink.essentials.config.HintValues;
import oracle.toplink.essentials.config.TopLinkQueryHints;

/**
 *
 * @author Administrator
 */
public class ObtenerNumeroTicketCommand implements Command<Long> {

    private Integer anio;

    public ObtenerNumeroTicketCommand(Integer anio) {
        this.anio = anio;
    }

    @Override
    public Long execute(EntityManager em) throws Exception {
        Long numero = 0L;
        try {
            numero = ((Long) em.createNamedQuery("Ticket.getNextNumero").setParameter("anio", anio).setHint(TopLinkQueryHints.REFRESH, HintValues.TRUE).getSingleResult());
        } catch (NoResultException e) {
            numero = 0L;
        }
        if (numero == null) {
            numero = 0L;
        }
        return numero;
    }
}
